import {guid} from '@/utils';

export const RelationType = {
  manual: 'manual', //手动选择
  auto: 'auto', //手动选择
  details: 'details', //新增明细行
};

export const RelationTypeTitle = {
  manual: '手工选择', //手动选择
  auto: '自动关联', //手动选择
  details: '新增明细行', //新增明细行
};

export function createRelation() {
  return {
    id: guid(),
    relationName: undefined,
    enable: true,
    relationSourceFormName: undefined,
    relationSourceFormId: undefined,

    relationTargetFormId: undefined, //关联单据的主表id
    relationTargetFormName: undefined, //关联单据的主表名称
    relationTargetInnerFormId: undefined, //主表 + 明细表的id, 逗号隔开
    relationType: RelationType.manual, //触发方式 RelationType

    manuals: [],
    auto: undefined,
    detail: undefined
  };
}

export function createDetailRelation() {//新增明细行
  return {
    relationId: undefined,
    conditionFormula: undefined,
    conditionFormulaTitle: undefined,
    queryFormula: undefined,
    queryFormulaTitle: undefined,
    autoRefreshRelationEditAuth: undefined, //编辑时权限 逗号隔开的权限列表
    autoRefreshRelationSaveAuth: undefined, //新增时权限 逗号隔开的权限列表
    autoFlushRelation: false, //是否待发刷新
    showRefreshRelationButton: false, //是否显示刷新关联的按钮
    refreshRelationButtonSaveAuth: false, //编辑时权限
    refreshRelationButtonEditAuth: false, //新增时权限
    refreshRelationButtonName: undefined, //刷新关联的按钮名称
    triggerFieldIds: undefined, //新增明细行触发字段(当这些字段变化的时候，触发一下明细表的自动关联)
    fieldBinds: [] //字段绑定
  };
}

export function createAuto() {
  return {
    relationId: undefined,
    conditionFormula: undefined,
    conditionFormulaTitle: undefined,
    notMatchedNotify: false, //关联未匹配到数据，给出用户提示（1 是 0 否）
    relationConditionQueryStrategy: '1', // （1任意一个为空时，按照该字段= null （默认）2 任意一个为空时，查询结果返回为空 ）
    concurrentControl: false, //是否并发控制
    autoRefreshRelationEditAuth: undefined, //编辑时权限（勾选了权限后且在该权限下打开表单时，系统会自动刷新一次该关联，编辑的权限）
    autoRefreshRelationSaveAuth: undefined,//新增时权限（勾选了权限后且在该权限下打开表单时，系统会自动刷新一次该关联, 新增的权限）
    autoFlushRelation: false, //是否待发刷新
    autoRelationQuery: [], //自动关联的查询条件
    fieldBinds: [], //字段绑定
    viewConfigs: [],
    previewFieldId: undefined //穿透字段的id
  };
}

export function createAutoRelationQuery() {
  return {
    formId: undefined,
    relationId: undefined,
    businessId: undefined,
    sourceFormId: undefined,
    sourceFormField: undefined,
    queryCondition: '=',
    sort: undefined,
    joinType: 'and',
    targetFormId: undefined,
    targetFormField: undefined,
  };
}

export function createManual() {
  return {
    id: guid(),
    relationId: undefined, //关联id
    manualSelectorFieldId: undefined, //选择器字段id
    manualSelectorButtonName: undefined, //手动筛选-选择器按钮名称（主表按钮）
    manualSelectorQueryFormula: undefined, //手动筛选-选择器筛选条件公式
    manualSelectorQueryFormulaTitle: undefined, //手动筛选-选择器筛选条件公式的翻译
    manualListPreview: false, //选择列表是否可穿透
    manualSelectorFieldPreview: false, //选择器字段是否可穿透
    manualEditTargetField: false, //是否允许编辑选择器字段
    manualSelectorType: false, //手动选择类型（1主表 2明细表）
    showManualSelectorSubFormButton: false, //是否显示：手动筛选-选择器按钮（明细表按钮）
    manualSelectorSubFormButtonName: false, //手动筛选-选择器按钮名称（明细表按钮）
    autoCallbackData: false, //是否自动回填明细表数据(自动回填功能待定)

    //选择器列表
    fieldColumns: [],
    //排序设置
    fieldSorts: [],
    //筛选条件
    fieldSearches: [],
    //关联映射
    fieldBinds: [],
    //列表视图配置
    listViewsConfigs: [],
    //字段穿透视图配置
    fieldViewConfigs: [],
  };
}

export function createViewConfig() {
  return {
    relationId: undefined,
    viewType: undefined,
    viewId: undefined,
    actionId: undefined
  }
}

export function createFieldColumns() {
  return {
    relationId: undefined,
    manualId: undefined,
    fieldId: undefined,
    formId: undefined,
    label: undefined,
    sort: undefined,
  };
}

export function createFieldSorts() {
  return {
    relationId: undefined,
    manualId: undefined,
    fieldId: undefined,
    formId: undefined,
    label: undefined,
    sortType: undefined,
    sort: undefined,
  };
}

export function createFieldSearches() {
  return {
    relationId: undefined,
    manualId: undefined,
    fieldId: undefined,
    formId: undefined,
    label: undefined,
    sort: undefined,
  };
}

export function createFieldBinds() {
  return {
    relationId: undefined,//关联id
    sourceFormId: undefined,//源表
    targetFormId: undefined,//目标表
    sourceFieldId: undefined,//源字段
    targetFieldId: undefined,//目标字段
    sort: undefined,//顺序
    fieldPreview: false,//选择器字段是否可穿透
    manualId: undefined, //手动选择id
  };
}